<?php
namespace Home\Controller;
use Think\Controller;
use Library\lunar;
class MyysController extends Controller {
    public function index(){
        indexRvent();//测算首页执行事件
        $this->assign('titleName',titleName('Myys',1));//首页名称
        cookie('orderidnum','Myys',604800);

        indexRequest($_REQUEST);//测算首页接收数据处理

        if(cookie('jploginHC')==''){
            cnzz_pvuv('MYYS');//执行统计
            cookie('jploginHC',1,30);
        }
        //渠道获取ID
        $channelID=obtain_channelID('MYYS');
        $this->assign('channelID',$channelID);

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            getToken();
            getTicket();
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
            wxShareUrl('MYYS','Myys');//分享地址获取

            if(!empty($_REQUEST['code'])){
                cookie('code',$_REQUEST['code'],7100);
            }
            getCode($wzurl);
            getOopenid();
            cookie('uid',cookie('openid'));
        }

        //渠道获取统计ID
        $channelID=obtain_channelID('MY');
        $this->assign('channelID',$channelID);

        //随机数
        csRandNum('myyscs',300000,499999);

        $monthNum=$this->getMonth();

        $this->assign('monthNum',$monthNum);
        $this->assign('month',intval(date('m')));
        $this->display('Myys/ZM_meiyue');
    }
    public function zhifu(){
        cookie('orderidnum','Myys',604800);
        $this->assign('titleName',titleName('Myys',2));//首页名称
        $strData=mb_substr(cookie('orderid'),0,2);
        if($strData=='QS')cookie('orderid',null);
        cookie('timeStrto',time()+7200);//倒计时时间戳
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);

            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){

            }else{
                getToken();
                getTicket();
            }
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
        }

        if($_REQUEST['username']){//返回操作
            cookie('znickname',I('post.username'),604800);
            cookie('zymd',mb_substr($_REQUEST['birthday'],2,10),7200);
            cookie('zhour',mb_substr($_REQUEST['birthday'],13,2),7200);
            cookie('zsex',$_REQUEST['gender'],604800);
            cookie('datetype',mb_substr($_REQUEST['birthday'],0,1));
            $typieMonth=$_REQUEST['typieMonth'];
        }
        if($_REQUEST['birthday']==''){
            $birthday='0-2018-08-08-8';
            cookie('zymd',$birthday);
        }

        if(!empty($_REQUEST['ordernum'])){//未支付订单
            $arrdata[0]=S('sm_h5test|'.$_REQUEST['ordernum']);
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            cookie('zhour',$arrdata[0]['hour'],604800);
            cookie('znickname',$arrdata[0]['username'],604800);
            cookie('zsex',$arrdata[0]['sex'],604800);
            cookie('datetype',$arrdata[0]['datetype']);

            cookie('orderid',$_REQUEST['ordernum'],604800);
        }

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='MYYS'.date('Ymd').mt_rand(10000,99999).date('His');
            cookie('orderid',$orderid,604800);
        }

        //创建订单
        $hour=cookie('zhour');if($hour=='')$hour=0;
        $shop=cookie('orderid');
        $znickname=cookie('znickname');if($znickname=='')$znickname='匿名';
        $zsex=cookie('zsex');if($zsex=='')$zsex=1;
        $channel=cookie('channel');
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $datetype=cookie('datetype');if($datetype=='')$datetype=0;
        defaultPrice();

        channelPay($channel,'MYYS');//查询渠道价格

        csPay($znickname,$channel);//测试价格判断

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
            $arrdata=S('sm_h5test|'.$shop);
            if(empty($arrdata)){
                $smH5testinter=array(
                    'ordernum'=>$shop, 'price'=>$price, 'username'=>$znickname, 'typeid'=>38, 'sex'=>$zsex, 'createtime'=>date('Y-m-d H:i:m'),
                    'status'=>-1, 'datetype'=>$datetype, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour,'minute'=>$typieMonth,'date_remark'=>'',
                    'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'], 'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0,
                );
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop);//支付页缓存订单操作

                $IndexController=A('Index');//Index控制器
                $IndexController->subNumber('MYYS');//提交订单次数统计
            }else if(!cookie($_REQUEST['username'])){//返回操作
                //更新订单
                $arrdata['username']=$znickname;$arrdata['datetype']=$datetype;
                $arrdata['year']=$y;$arrdata['month']=$m;$arrdata['day']=$d;$arrdata['sex']=$zsex;
                S('sm_h5test|'.$shop,$arrdata,2592000);//2592000为30天
            }
        }

        if(cookie('zsex')==1){$zsex='男';}else{$zsex='女';}
        zwmp(cookie('zymd'), cookie('zhour'),$zsex,'sizhu');
        $sizhu=I('globals.sizhu');
        $this->assign('sizhu',$sizhu);//Z四柱

        if($datetype==1){
            $this->assign('datetype',"农历 ".cookie('zyinli')." ".mb_substr($sizhu[3],1,1,'utf-8')."时");
        }else{
            $this->assign('datetype',"阳历 ".cookie('zyangli'));
        }

        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
        }else{
            $xml=wxgzhzf('MYYS','每月运势',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));
        }

        $monthNum=$this->getMonth();

        $this->assign('monthNum',$monthNum);
        $this->assign('csNameJump',U('Myys/jieguoye','',false).'?ordernum='.cookie('orderid'));//支付成功跳转地址
        $this->assign('channel',cookie('channel'));
        $this->assign('znickname',cookie('znickname'));
        $this->assign('sex',cookie('zsex'));
        $this->assign('zhifuType',1);//特别支付类型

        if(channeFree($channel)){
            $this->display('Myys/ZM_meiyue_zhifu_free');
        }else{
            $this->display('Myys/ZM_meiyue_zhifu');
        }
    }
    function jieguoye(){
        jieguoInterface();//结果页公共函数
        $this->assign('titleName',titleName('Myys',3));//首页名称
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");
            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
        }

        $ordernum=$_REQUEST['ordernum'];
        $arrdata[0]=jieguoyeArrData($ordernum);

        $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
        cookie('zymd',$zymd,604800);
        cookie('zhour',$arrdata[0]['hour'],604800);
        cookie('znickname',$arrdata[0]['username'],604800);
        cookie('zsex',$arrdata[0]['sex'],604800);
        $hour=$arrdata[0]['hour'];
        $typeMonth=$arrdata[0]['minute'];
        $createtime=$arrdata[0]['createtime'];
        $createYear=substr($createtime,0,4);

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历
        $zymd= cookie('zymd');
        cookie('orderid',$arrdata[0]['ordernum'],604800);

        jieguoyeRequest($arrdata[0]['status']);//结果页公共处理

        if(cookie('zsex')==1){
            $zsex='男';
        }else{
            $zsex='女';
        }
        if($zymd!=''){
            zwmp($zymd,$hour,$zsex,'yc');
            $shengxiao=I('globals.shengxiao');
            $this->assign('shengxiao',I('globals.shengxiao'));//生肖
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $ssarray=array();
            array_push($ssarray,implode(I('globals.nianzhuss')));
            array_push($ssarray,implode(I('globals.yuezhuss')));
            array_push($ssarray,implode(I('globals.shizhuss')));
            $this->assign('nianzhuss',$ssarray);//十神
            $this->assign('canggan',I('globals.canggan'));//藏干

            $this->assign('zhishen',I('globals.zhishen'));//支神
            $this->assign('sizhuny',I('globals.sizhuny'));//四柱纳音
            $this->assign('bzdishi',I('globals.bzdishi'));//八字地势
            $this->assign('wxsqs',I('globals.wxsqs'));//旺相休囚死
            $this->assign('xiys',I('globals.xiys'));//喜用神
            $this->assign('taiyuan',I('globals.taiyuan'));//胎元
            $this->assign('rikong',I('globals.rikong'));//日空
            $this->assign('dayun',I('globals.dayun'));//大运
            $this->assign('dayunx',I('globals.dayunx'));//大运x
            $this->assign('dayuny',I('globals.dayuny'));//大运y
            $this->assign('dayungz',I('globals.dayungz'));//大运干支
            $this->assign('dayunss',I('globals.dayunss'));//大运十神


            if($createYear==2020&&$typeMonth>=5){
                $MyysData=new \Library\Myys2020Data();
            }else{
                $MyysData=new \Library\MyysData();
            }
            if($typeMonth==4){
                $MyysArr=$MyysData->AprilData($shengxiao);
            }else if($typeMonth==5){
                $MyysArr=$MyysData->MayData($shengxiao);
            }else if($typeMonth==6){
                $MyysArr=$MyysData->JuneData($shengxiao,$zymd,$hour);
            }elseif($createYear==2020&&$typeMonth>=7){
                $MyysArr=$MyysData->monthly($zymd,$hour,$typeMonth);//每月运势
            } else if($typeMonth==7){
                $MyysArr=$MyysData->JulyData($shengxiao);
            }else if($typeMonth==8){
                $MyysArr=$MyysData->August($shengxiao);
            }else if($typeMonth==9){
                $MyysArr=$MyysData->September($shengxiao);
            } else if($typeMonth==10){
                $MyysArr=$MyysData->October($shengxiao);
            }else if($typeMonth==11){
                $MyysArr=$MyysData->November($shengxiao);
            }elseif($typeMonth==12){
                $MyysArr=$MyysData->December($shengxiao);
            }elseif($typeMonth==1){
                $MyysArr=$MyysData->January($shengxiao);
            }elseif($typeMonth==3){
                $MyysArr=$MyysData->March($shengxiao);
            } else{
                $zhanxing=new \Library\ZhanXing();
                $constellation=$zhanxing->constellation($m,$d);//获取星座
                $MyysArr=$MyysData->Data($constellation);
            }


            $this->assign('MyysArr',$MyysArr);
            //https://www.d1xz.net/sx/   数据地址
        }

        //if($typeMonth>=4){
            $this->display('Myys/ZM_meiyue_jieguoye_april');
//        }else{
//            $this->display('Myys/ZM_meiyue_jieguoye');
//        }
    }

    public function pay(){//支付宝支付
        $orderid=cookie('orderid');
        $price=cookie('price');

        $this->assign('ordername1','每月运势');
        $this->assign('ordername2','知命-每月运势');
        $this->display("Index/pay");
    }
    public function paywx(){//H5微信支付
        H5ZMwxzhifu('MYYS','每月运势',cookie('orderid'),'Myys');//知命
    }

    public function getMonth(){//获取测算月份
        $month=intval(date('m'));
        $day=date('d');
        if($day>=25){
            $month+=1;
        }
        $nameArr=array(
            7=>'七',
            8=>'八',
            9=>'九',
            10=>'十',
            11=>'十一',
            12=>'十二',
        );
        $Arr=array(
            $month,$nameArr[$month]
        );
        return $Arr;
    }

    public function selectOrder(){//缓存查询
        $arrdata[0]=S('sm_h5test|'.$_REQUEST['ordernum']);
        dump($arrdata);
    }


}
